package com.woniuxy.controller;

import com.woniuxy.pojo.User;
import com.woniuxy.service.MyLog;
import com.woniuxy.service.UserService;
import com.woniuxy.util.ResultDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
public class LoginController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserService userService;
    @PostMapping(value = "/login.do")
    public ResultDto login(String telephone, String password,
                           @RequestParam(value = "isRememberMe",defaultValue = "0") Integer isRememberMe,
                           HttpServletResponse response, HttpServletRequest request){
        String pas1=password;
        Subject subject=SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(telephone,password);
        //记住我后存入cookie
        if(isRememberMe==1){
            token.setRememberMe(true);
            try {
                subject.login(token);
                if(subject.isAuthenticated()){
                    User user=this.userService.findUserByTel(telephone);
                    user.setPassword(pas1);
                    //获取Session对象
                   Session session=subject.getSession();
                    session.setAttribute("USER_SESSION",user);
                    logger.debug("存入时候的密码："+user.getPassword());
                    Cookie cookie1=new Cookie("telephone",user.getTelephone());
                    Cookie cookie2=new Cookie("password",user.getPassword());
                    //存储时间
                    cookie1.setMaxAge(60*60*24*7);
                    cookie2.setMaxAge(60*60*24*7);
                    cookie1.setPath("1");
                    cookie2.setPath("1");
                    response.addCookie(cookie1);
                    response.addCookie(cookie2);
                    return ResultDto.LOGIN_SUCCESS;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultDto.LOGIN_FAILURE;
            }
        }
           //否则只是存入session
        try {
                subject.login(token);
                if(subject.isAuthenticated()){
                    User user=this.userService.findUserByTel(telephone);
                   //存入明文密码
                    user.setPassword(pas1);
                    //获取Session对象
                    Session session=subject.getSession();
                    session.setAttribute("USER_SESSION",user);
                    return ResultDto.LOGIN_SUCCESS;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultDto.LOGIN_FAILURE;
            }
        return null;
    }
}
